
/**
 * 深拷贝对象
 * @param {对象} source 
 */
function deepClone(source) {
  if (!source && typeof source !== "object") {
    throw new Error("error arguments", "deepClone");
  }
  const targetObj = source.constructor === Array ? [] : {};
  Object.keys(source).forEach((keys) => {
    if (source[keys] && typeof source[keys] === "object") {
      targetObj[keys] = this.deepClone(source[keys]);
    } else {
      targetObj[keys] = source[keys];
    }
  });
  return targetObj;
}

/**
 * 复制内容到剪切板
 * @param {拷贝内容} text 
 * @param {回调方法} callback 
 */
function copyText(text, callback) {
  // let input = document.createElement('input') input 无法实现换行
  let input = document.createElement('textarea')
  input.setAttribute('readonly', 'readonly') // 防止手机上弹出软键盘
  // input.setAttribute('value', text)
  input.innerHTML = text
  document.body.appendChild(input)
  input.select()
  document.execCommand('copy')
  document.body.removeChild(input)
  if (callback) callback(text)
}

/**
 * 刷新值
 * @param {控制对象显示值} is 
 */
// eslint-disable-next-line no-unused-vars
function refreshObj(_this, key) {
  _this[key] = false
  _this.$nextTick(() => {
    _this[key] = true
  })
}

export default {
  deepClone,
  copyText,
  refreshObj
}